package springboot.controller;

import entity.Student;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import springboot.config.UserProperties;
import springboot.dao.UserDao;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@RestController
@MapperScan("springboot.dao")
@EnableConfigurationProperties(UserProperties.class)
public class DemoController {

    @Resource
    private UserDao userDao;

    @Resource
    private UserProperties userProperties;


    @Resource
    private DataSource dataSource;

    @GetMapping("/dao")
    public String dao(){
        Student student = userDao.listStudent();
        return "1";
    }


    @GetMapping("/t")
    public String t() throws SQLException {

        Connection connection = dataSource.getConnection();
        String insertSql = "insert into student_test_merge_sql (`id`,`name`,`age`,`cr`,`no`) values ";

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(insertSql);
        String formatValues = "(%s,%s,%s,%s,%s)";
        for (int i = 0; i < 2000000; i++) {
            stringBuilder.append(String.format(formatValues, null, i, i, i, i));

            if (i % 1000==0) {
                stringBuilder.append(";");
                PreparedStatement preparedStatement = connection.prepareStatement(stringBuilder.toString());
                preparedStatement.execute();
                stringBuilder = new StringBuilder(insertSql);
            } else {
                stringBuilder.append(",");
            }
        }


        System.out.println(connection);

        System.out.println(userProperties.getUsername());
        System.out.println(userProperties.getPassword());

        return  userProperties.getPassword();
    }
}
